package network;/*
 *@ 代阳敲的专属代码
 **/

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    Socket socket;

    //和服务器通信，就要知道服务器的ip和端口
    public TcpEchoClient(String serverIp,int port) throws IOException {
        socket=new Socket(serverIp,port);
    }
    public void start() {
        System.out.println("客户端启动");
        Scanner scannerConsle =new Scanner(System.in);


        try {
            InputStream inputStream=socket.getInputStream();
            OutputStream outputStream=socket.getOutputStream();
            while(true) {
                //从控制台输入字符
                System.out.println("->");
                String request=scannerConsle.next();
                //把请求发送给服务端
                PrintWriter printWriter=new PrintWriter(outputStream);

                printWriter.println(request);
                //刷新缓存区
                printWriter.flush();
                //从服务端返回数据
                Scanner sannerNetwork =new Scanner(inputStream);
                String response= sannerNetwork.next();
                System.out.println(response);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient client =new TcpEchoClient("127.0.0.1",8080);
        client.start();
    }
}
